package com.xiao.sulinguser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiao.sulingpublic.to.MemberAddressTo;
import com.xiao.sulingpublic.utils.PageUtils;
import com.xiao.sulingpublic.utils.R;
import com.xiao.sulinguser.entity.MemberAddressEntity;
import com.xiao.sulinguser.service.MemberAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 用户收货地址表
 *
 * @author xiao
 * @email 1322127758@qq.com
 * @date 2022-03-23 21:37:31
 */
@RestController
@RequestMapping("sulinguser/memberaddress")
public class MemberAddressController {
    @Autowired
    private MemberAddressService memberAddressService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("sulinguser:memberaddress:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberAddressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{maId}")
    //@RequiresPermissions("sulinguser:memberaddress:info")
    public R info(@PathVariable("maId") Long maId){
		MemberAddressEntity memberAddress = memberAddressService.getById(maId);

        return R.ok().put("memberAddress", memberAddress);
    }

    /**
     *
     */
    @GetMapping("/list/address")
    public R selectByUsername(@RequestParam("maUsername") String maUsername){

        List<MemberAddressEntity> memberAddressList = memberAddressService.list(new QueryWrapper<MemberAddressEntity>().eq("ma_username", maUsername));
        return R.ok().put("memberAddressList",memberAddressList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("sulinguser:memberaddress:save")
    public R save(@RequestBody MemberAddressEntity memberAddress){
		memberAddressService.save(memberAddress);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/saveAddress")
    //@RequiresPermissions("sulinguser:memberaddress:save")
    public R saveAddress(@RequestBody MemberAddressTo memberAddress){
        MemberAddressEntity memberAddressEntity = new MemberAddressEntity();
        BeanUtils.copyProperties(memberAddress,memberAddressEntity);
		memberAddressService.save(memberAddressEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("sulinguser:memberaddress:update")
    public R update(@RequestBody MemberAddressEntity memberAddress){
		memberAddressService.updateById(memberAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("sulinguser:memberaddress:delete")
    public R delete(@RequestBody Long[] maIds){
		memberAddressService.removeByIds(Arrays.asList(maIds));

        return R.ok();
    }

}
